#include "applicatonengine.h"

ApplicatonEngine::ApplicatonEngine(QObject *parent)
    : QQmlApplicationEngine{parent}
{

    m_MaterialHeightManager = new Material_Height_Manager(this);
    // 初始化 料高数据库
    m_MaterialHeightManager->isExistTable();
    m_OrderProcessManager = new Order_Process_Manager(this);
    // 初始化 本地料单数据库
    m_OrderProcessManager->isExistTable();

    m_Config = new Config(this);

    m_Language = new LanguageControl(this);
    m_Language->setCurrentType(m_Config->Language_Type());    
    m_Language->load(m_Language->CurrentType());

    m_Scanner = ScannerControl::getInstance();
    m_Printer = PrinterControl::getInstance();
    m_NetManager = NetManager::getInstance();
    m_Scanner->setScannerConnectParameter(m_Config->scanner_Port(),m_Config->scanner_BaudRate(),m_Config->scanner_DataBit(),m_Config->scanner_Parity(),m_Config->scanner_Stopbit());
    m_Printer->setPrinterConnectParameter(m_Config->printer_Port(),m_Config->printer_BaudRate(),m_Config->printer_DataBit(),m_Config->printer_Parity(),m_Config->printer_Stopbit());
    m_Scanner->connectToDevice();
    m_Printer->connectToDevice();

    m_PLC = ModbusControl::getInstance();

    m_PLC->setModbusConnectParameter(m_Config->plc_Port(),m_Config->plc_BaudRate(),m_Config->plc_DataBit(),m_Config->plc_Parity(),m_Config->plc_Stopbit());
    m_PLC->connectToDevice();

    m_Scanner->setCurrent_QR_Type(m_Config->QR_Code_Type());
    m_PLC->setQR_Code_CurrentType(m_Config->QR_Code_Type());

    // 切割数据Model
    m_CutModel = CutDataModel::getInstance();

    // U盘料单数据Model
    m_USB_Process_Model = USB_ProcessList_Model::getInstance();

    // 注册
    registerMethod();

    m_Printer->setCurrentType(m_Config->Printer_Type());
    if(m_Config->Language_Type() == 1)
    {
        m_Printer->setLanguage_Type(true);
    }
    else
    {
        m_Printer->setLanguage_Type(false);
    }

    // 扫码枪 与 PLC交互信号绑定
    QObject::connect(m_Scanner,&ScannerControl::send_Scanner_Result_OutLine_Signal,m_PLC,&ModbusControl::on_Scanner_Read_Result_Slots);

    // 扫码枪 与 网络料单处理类 交互信号绑定
    QObject::connect(m_Scanner,&ScannerControl::send_Scanner_Result_ChangFeng_Signal,m_NetManager,&NetManager::recv_Scanner_Result_ChangFeng_Slots);
    QObject::connect(m_Scanner,&ScannerControl::send_Scanner_Result_XinGeEr_Signal,m_NetManager,&NetManager::recv_Scanner_Result_XinGeEr_Slots);
    QObject::connect(m_Scanner,&ScannerControl::send_Scanner_Result_SuZhi_Signal,m_NetManager,&NetManager::recv_Scanner_Result_SuZhi_Slots);
    QObject::connect(m_Scanner,&ScannerControl::send_Scanner_Result_ChuangPin_Signal,m_NetManager,&NetManager::recv_Scanner_Result_ChuangPin_Slots);

    //打印机与PLC交互
    QObject::connect(m_PLC,&ModbusControl::printer_Active_Sig,m_Printer,&PrinterControl::on_AutoActiveSlots);
}

ApplicatonEngine::~ApplicatonEngine()
{

    m_Config->save_All_Data();

}

void ApplicatonEngine::registerMethod()
{

    // 料长数据库对象
    this->rootContext()->setContextProperty("Cpp_MaterialHeight",m_MaterialHeightManager);

    // 二维码料单数据库对象
    this->rootContext()->setContextProperty("Cpp_OrderProcess",m_OrderProcessManager);

    // 配置文件对象
    this->rootContext()->setContextProperty("Cpp_Config",m_Config);

    // PLC 控制类
    this->rootContext()->setContextProperty("Cpp_PLC",m_PLC);

    // 打印机控制类
    this->rootContext()->setContextProperty("Cpp_Printer",m_Printer);

    // 扫码枪控制类
    this->rootContext()->setContextProperty("Cpp_Scanner",m_Scanner);

    // 料单数据
    this->rootContext()->setContextProperty("cutDataModel",m_CutModel);

    // U盘料单数据
    this->rootContext()->setContextProperty("usbCutDataModel",m_USB_Process_Model);

    // 网络管理类
    // this->rootContext()->setContextProperty("Cpp_NetWork",m_NetManager);

    // 语言控制类
    this->rootContext()->setContextProperty("Cpp_Language",m_Language);

}

